package com.xdm.ui.adapter.android;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.xdm.ui.XmDecoratedText;


public class AndroidDecoratedTextListAdapter extends BaseAdapter {

    /**
     * Remember our context so we can use it when constructing views.
     */
    private Context context;

    private List<XmDecoratedText> itemList;

    /**
     * 
     * @param context
     */
    public AndroidDecoratedTextListAdapter(Context context) {
	this.context = context;
	itemList  = new ArrayList<XmDecoratedText>();
    }

    /**
     * 
     * @param bt
     */
    public void addItem(XmDecoratedText bt) {
	itemList.add(bt);
    }

    /**
     * The number of items in the
     */
    public int getCount() {
	return itemList.size();
    }

    /**
     * 
     */
    public Object getItem(int position) {
	return itemList.get(position);
    }

    /**
     * 
     */
    public boolean areAllItemsSelectable() {
	return false;
    }

    /**
     * 
     */
    public boolean isSelectable(int position) {
	return itemList.get(position).isSelectable();
    }

    /**
     * Use the array index as a unique id.
     */
    public long getItemId(int position) {
	return position;
    }

    /**
     * Make a DecoratedTextView to hold each row.
     */
    public View getView(int position, View convertView, ViewGroup parent) {
	AndroidDecoratedTextView btv;
	XmDecoratedText decoratedText = itemList.get(position);
	String text = decoratedText.getText();
	
	Drawable drawable = (Drawable)((AndroidImage)decoratedText.getDecoration()).implementation;
	if (convertView == null) {
	    btv = new AndroidDecoratedTextView(context, text, drawable);
	} else {
	    btv = (AndroidDecoratedTextView) convertView;
	    btv.setText(text);
	    btv.setDecoration(drawable);
	}

	return btv;
    }
}